package cn.edu.scau.cmi.wujiaxian.exercise9;

import java.util.Calendar;

public class CalendaBean {

	String[] day;
	int year = 0;
	int month = 0;
	public String[] getDay() {
		return day;
	}
	public void setDay(String[] day) {
		this.day = day;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	
	public String[] getCalendar() {
		String[] a = new String[42];
		Calendar rili = Calendar.getInstance();
		rili.set(year, month - 1, 1);
		int weekDay = rili.get(Calendar.DAY_OF_WEEK) - 1;
		int day = 0;
		if (month == 1 || month == 3 || month == 5 || 
				month == 7 || month == 8 || month == 10 || month == 12) {
			day = 31;
		} else if (month == 2) {
			if ((year % 4 ==0) && (year % 100 != 0) || (year % 400) == 0) {
				day = 29;
			} else {
				day = 28;
			}
		} else {
			day = 30;
		}
		for (int i = 0; i < weekDay; i++) {
			a[i] = " ";
		}
		for (int i = weekDay, n = 1; i < weekDay + day; i++) {
			a[i] = String.valueOf(n);
			n++;
		}
		for (int i = weekDay + day; i < a.length; i++) {
			a[i] = " ";	
		}
		return a;
	}
	
	public void rili(int year, int month) {
		CalendaBean cb = new CalendaBean();
		cb.setYear(year);
		cb.setMonth(month);
		String[] a = cb.getCalendar();
		char[] str = "日一二三四五六".toCharArray();
		for (char c : str) {
			System.out.printf("%8c",c);
		}
		for (int i = 0; i < a.length; i++) {
			if (i % 7 == 0) {
				System.out.println("");
			} 
			System.out.printf("%4s", a[i]);
		}
	}
	
}
